package user;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import fj.F;
import global.Global;

import server.Initialize;
import server.db.DatabaseFunctor;
import server.db.StmtHelper;
import util.Validation;

import ajaxmethods.AjaxMethod;
import ajaxmethods.SessionMethod;

public class CreateAccount extends SessionMethod {

	public static String KAPTCHA_ERROR="The characters entered do not match";
	private List<String> requiredFields = Arrays.asList("firstname","lastname","email","password","password2","kaptcha");
	private Map<String,String> fieldsMap = new HashMap();
	private String kaptchakey;
	
	@Override
	public void perform(HttpServletRequest request, HttpServletResponse response) {
		super.perform(request, response);
		StringBuffer sb = new StringBuffer();
		String redirect = request.getParameter("redirect");
		sb.append("<div id=\"smalldlg\"><h3 class=\"bubbleheader\">Create An Account</h3>" +
		"<div id=\"name\" style=\"height:60px;\"><div class=\"floatInputWrapper\" >" +
		"<input type=\"hidden\" name=\"redirect\" value=\"/"+(redirect!=null?redirect:"")+"\"><p class=\"textbox halfleft\">"+
	    "<label for=\"firstname\">First Name</label>"+
	    "<input type=\"text\" id=\"firstname\" class=\"esoinput\" name=\"firstname\" value=\"\" title=\"firstname\" tabindex=\"1\">"+
	    "<p class=\"textbox halfright\">"+
	    "<label for=\"lastname\">Last Name</label>"+
	    "<input type=\"text\" id=\"lastname\" class=\"esoinput\" name=\"lastname\" value=\"\" title=\"lastname\" tabindex=\"2\">"+
	    "</div></div><div id=\"email\"><p class=\"textbox\">"+
	    "<label for=\"email\">Email</label>"+
	    "<input type=\"text\" class=\"esoinput\" name=\"email\" value=\"\" title=\"username\" tabindex=\"4\">"+
	    "</p><div class=\"errorText\">&nbsp;</div></div>" +
	    "<div id=\"password\"><p class=\"textbox\">"+
	    "<label for=\"password\">Password</label>"+
	    "<input type=\"password\" class=\"esoinput\" name=\"password\" value=\"\" title=\"password\" tabindex=\"5\">"+
	    "</p>" +
	    "<p class=\"textbox\">"+
	    "<label for=\"password\">Re-enter Password</label>"+
	    "<input type=\"password\" class=\"esoinput\" name=\"password2\" value=\"\" title=\"re-enter password\" tabindex=\"5\">"+
	    "</p><div class=\"errorText\">&nbsp;</div></div>" +
	    "<div id=\"kaptcha\"><p class=\"textbox\">"+
	    "<label for=\"kaptcha\">Type the characters you see in the picture below</label>"+
	    "<img style=\"border-radius:5px;-webkit-border-radius:5px;-moz-border-radius:5px;\" src=\""+Initialize.appURL()+"/common/kaptcha.jpg\" />" +
	    "<input type=\"text\" class=\"esoinput\" name=\"kaptcha\" value=\"\"  title=\"word verification\" tabindex=\"5\">"+
	    "<div class=\"errorText\">&nbsp;</div></p></div><button class=\"esobutton\" id=\"createAcct\" onclick=\"submitAcct()\" title=\"create account\" type=\"submit\">Create Account</button></div>");
		
		try {
			data.put("htmlSelector", "#workspace").put("htmlValue", sb.toString()).write(response.getWriter());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	

	
	public StringBuffer createAcctJavascript(String redirect)
	{
		StringBuffer sb = new StringBuffer();
		sb.append("<script>function createAcct() {" +
				"	$('#loginDlg').hide();"+
				"	$('#main').data('bubble').close();");
		if(redirect!=null & redirect.length()>0)
			sb.append("	$.address.value('/createaccount/?redirect=map');");
		else
			sb.append("	$.address.value('/createaccount');");
		sb.append("}</script>");
		return sb;
	}

}
